package bbs.net;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bbs.model.CommitBack;
import managers.VolleySingleton;

/**
 * 获取所有回复
 * Created by nimi on 2015/11/17.
 */
public class GetCommitBack {
    Long submitID;
    Download download;
    Delegate delegate;

    public GetCommitBack(Long submitID) {
        this.submitID = submitID;
    }


    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public Delegate getDelegate() {
        return delegate;
    }

    public Download getDownload() {
        return download;
    }

    public void setDownload(Download download) {
        this.download = download;
    }

    public void get() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/BBS/GetAllFeedbackOfBoard";
        RequestQueue requestQueue = VolleySingleton.getVolleySingleton(MyApplication.share()).getRequestQueue();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            public void onResponse(String response) {
                afterGet(response);
                if (delegate != null)
                    delegate.onResponse(response, GetCommitBack.this);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error, GetCommitBack.this);

            }
        })

        {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("board_ID", submitID + "");
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }

        };
        requestQueue.add(stringRequest);
    }

    private void afterGet(String response) {
        download = JSON.parseObject(response, Download.class);
    }

    // 下行數據
    public static class Download {
        // 状态
        int status;

        // 所有的回复
        List<BoardFeedback> bbs_Feedback = new ArrayList<BoardFeedback>();

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public List<BoardFeedback> getBbs_Feedback() {
            return bbs_Feedback;
        }

        public void setBbs_Feedback(List<BoardFeedback> bbs_Feedback) {
            this.bbs_Feedback = bbs_Feedback;
        }
    }

    public static interface Delegate {
        public void onResponse(String response, GetCommitBack getCommitBack);

        public void onErrorResponse(VolleyError error, GetCommitBack getCommitBack);
    }

    public static class BoardFeedback {
        private String feedback_Content;
        private String feedback_Time;
        private String user_ID;
        private String user_RealName;
        private String board_ID;

        public String getFeedback_Content() {
            return feedback_Content;
        }

        public void setFeedback_Content(String feedback_Content) {
            this.feedback_Content = feedback_Content;
        }

        public String getFeedback_Time() {
            return feedback_Time;
        }

        public void setFeedback_Time(String feedback_Time) {
            this.feedback_Time = feedback_Time;
        }

        public String getUser_ID() {
            return user_ID;
        }

        public void setUser_ID(String user_ID) {
            this.user_ID = user_ID;
        }

        public String getUser_RealName() {
            return user_RealName;
        }

        public void setUser_RealName(String user_RealName) {
            this.user_RealName = user_RealName;
        }

        public String getBoard_ID() {
            return board_ID;
        }

        public void setBoard_ID(String board_ID) {
            this.board_ID = board_ID;
        }
    }
}
